//
//  JWKStatusPictureView.swift
//  Weibo
//
//  Created by 姜维克 on 16/5/25.
//  Copyright © 2016年 姜维克. All rights reserved.
//

import UIKit


//最大宽度
    private let rowMaxWidth = UIScreenWidth - 2*standardMargin
//cell间距
    private let cellMargin : CGFloat = 3
//单张图片宽度
    private let itemWidth = (rowMaxWidth - 2 * cellMargin) / 3
//不能写到里面!!,上面一句会报错

//布局对象
//    private var flowLayout : UICollectionViewFlowLayout?

//cell的重用id
private let pictureCellId = "pictureCellId"

class JWKStatusPictureView: UICollectionView {
    
   private var flowLayout : UICollectionViewFlowLayout?
    var pictures : [JWKStatusPicture]?{
        didSet{
           
            contentLabel.text = "\(pictures?.count)"
            
            let viewSize = cacaulateSize()
            
            
            //MARK:-不能用make在这里,需要要update,不然又约束冲突,不显示view
//            "<Weibo.JWKStatusPictureView:0x7fbd1a108000.height == 0.0>",
//            "Weibo.JWKStatusPictureView:0x7fbd1a108000.height == 150.0>"
            self.snp_updateConstraints { (make) in
                make.size.equalTo(viewSize)
            }
            
            //刷新视图数据
            self.reloadData()
            
        }
    }
    
    
    lazy var contentLabel : UILabel = UILabel.init(title: "测试", textColor: UIColor.darkGrayColor(), textAlignment: .Left, font: 15)
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flow : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        flowLayout = flow
        //设置间距
        flow.minimumLineSpacing = cellMargin
        flow.minimumInteritemSpacing = cellMargin
        flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
        //MARK:-itemSize不要固定死,应该根据图片的张数去改变
//        flow.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        super.init(frame: frame, collectionViewLayout: flow)
        
        backgroundColor = UIColor.purpleColor()
        
        
        //MARK:关闭collectionView的scrollToTop,因为屏幕上只有一个滚动视图的该属性是true时候,才能点击 状态栏 回到最上面,而且默认滚动视图的该属性是true的
        scrollsToTop = false
        //设置数据源
        dataSource = self
        
        //注册cell
        registerClass(JWKPictureCell.self, forCellWithReuseIdentifier: pictureCellId)
        
        
        
        setupUI()
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        addSubview(contentLabel)
        
        //设置label的最大宽度
        contentLabel.preferredMaxLayoutWidth = UIScreenWidth - 2*standardMargin
        
        contentLabel.snp_makeConstraints { (make) in
            make.center.equalTo(self)
            
        }
        
        
        //底部间距约束
//        self.snp_makeConstraints { (make) in
//            make.bottom.equalTo(contentLabel.snp_bottom).offset(standardMargin)
//        }
        
    }

    
    private func cacaulateSize()->CGSize{
        //计算尺寸
        let imageCount = pictures?.count ?? 0
        
//        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
//        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        flowLayout!.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        if imageCount == 0 {
            return CGSizeZero
        }else if imageCount == 1 {
            //TODO:真实尺寸显示
            let viewSize = CGSizeMake(100, 150)
//            layout.itemSize = viewSize
            flowLayout!.itemSize = viewSize
           return viewSize
            
        }else if imageCount == 4{
            //2*2
            let viewWidth = itemWidth * 2 + cellMargin
            return CGSizeMake(viewWidth, viewWidth)
            
        }else{
            //其余的多张图片
            //行数
            //MARK:-CGFloat转换类型一定要尽量包住所有的运算式,防止出错
             //CGFloat(imageCount - 1)/3 + 1是错误的
            let row = CGFloat((imageCount - 1)/3) + 1
            //MARK:行数后面要加1,前面图片数减1,才能正确得出行数
            let viewHidth = row * itemWidth + (row - 1)*cellMargin

            return CGSizeMake(rowMaxWidth, viewHidth)
            
        }

    }
    

}


extension JWKStatusPictureView : UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictures?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! JWKPictureCell
        
        cell.picture = pictures![indexPath.row]
        
//        collectionView.collectionViewLayout.item
        
        return cell
    }
}

//自定义cell
class JWKPictureCell: UICollectionViewCell{
    
    var picture : JWKStatusPicture?{
        didSet{
            let url = NSURL(string: picture?.thumbnail_pic ?? "")
            imageView.sd_setImageWithURL(url)
        }
    }
    
    lazy var imageView : UIImageView = UIImageView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
    
        contentView.addSubview(imageView)
        
        imageView.contentMode = .ScaleAspectFill
        //手动写代码需要自己切除,xib和sb中会自动切除
        imageView.clipsToBounds = true
        
        imageView.snp_makeConstraints { (make) in
            make.edges.equalTo(self.contentView)
        }
    }
    
    
}

